<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>WebSocket Chat</title>
</head>
<body>
<textarea id="chatArea" rows="10" cols="50"></textarea><br>
<input type="text" id="messageInput" placeholder="Type your message">
<button onclick="sendMessage()">Send</button>

<!--           访问http://localhost:9090/index.html                -->
<script>
    // 如果不是https，就用ws://
    var socket = new WebSocket("wss://localhost:8080/chat");

    // 建立socket连接
    socket.onopen = function(event) {
        console.log("WebSocket connected.");
    };

    //监听消息
    socket.onmessage = function(event) {
        var chatArea = document.getElementById("chatArea");
        chatArea.value += event.data + "\n";
    };

    // 关闭socket连接
    socket.onclose = function(event) {
        console.log("WebSocket closed.");
    };

    // 发送消息
    function sendMessage() {
        var messageInput = document.getElementById("messageInput");
        var message = messageInput.value;
        console.log("发送消息：",message);
        socket.send(message);
        messageInput.value = "";
    }
</script>
</body>
</html>